<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'base.item.class.php');

class URLBase extends BaseItemBase {
	var $_ignore = 0;
	var $_status = 0;
	
	//constructor
	function URLBase() {
		parent::BaseItemBase();
		
		$this->_table_owner_usable = false;
		$this->_table_status_usable = false;
		
		$this->item_link = true;
		$this->table = 'pp_urls';
		$this->prefix = 'urls_';
		
		$this->search_fields = array('link', 'value');
		$this->sort_fields = array('link+', 'value+');
	}
	
	//methods
	function sync() {
		//clean
		$query = "TRUNCATE TABLE pp_urls";
		$this->adb->query($query);
		
		//content/article
		$query = "insert into pp_urls (urls_link, urls_type, urls_value) " 
				. " select contents_link, 'content', contents_link from pp_contents where 1";
		$this->adb->query($query);
		
		//product
		$query = "insert into pp_urls (urls_link, urls_type, urls_value) " 
				. " select products_link, 'product', products_link from pp_products where 1";
		$this->adb->query($query);
		
		//news
		$query = "insert into pp_urls (urls_link, urls_type, urls_value) " 
				. " select news_link, 'news', news_link from pp_news where 1";
		$this->adb->query($query);
		
		//page
		$query = "insert into pp_urls (urls_link, urls_type, urls_value) " 
				. " select pages_link, 'page', pages_link from pp_pages where 1";
		$this->adb->query($query);
		
		//
		return false;
	}
	
	function dispatch() {
		$this->_bind();
		$this->_check();
		if( $this->_ignore == 0 ) {
			$this->_process();
		}
		if( $this->_status == 1 ) {
			$this->_publish();
		}
	}
	
	function _bind() {
		global $request_uri, $request_type, $request_link, $request_link_class, $request_link_subclass;
		global $request_param, $request_link_param;
		
		$this->request_uri = $request_uri;
		$this->request_type = $request_type;
		$this->request_link = $request_link;
		$this->request_link_class = $request_link_class;
		$this->request_link_subclass = $request_link_subclass;
		$this->request_param = $request_param;
		$this->request_link_param = $request_link_param;
	}
	
	function _check() {
		if( in_array($this->request_type, array('a', 'article', 'c', 'content', 'n', 'news', 'page', 'p', 'product')) ) {
			$this->_ignore = 1;
		}
	}
	
	function _process() {
		$uri = $this->_process_request_uri(true);
		$item = $this->getItemBy($uri, 'link');
		if( $item['id'] == 0 ) {
			$uri = $this->_process_request_uri(false);
			
			include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/content/content.class.php');
			$mObj = new ContentBase();
			$mObj->format = true;
			$_item = $mObj->getItem($uri, 'link');
			
			$item = array();
			$item['type'] = 'a';
			$item['link'] = $_item['link'];
			$item['value'] = $_item['link'];
		}
		if( $item['link'] != '' ) {
			$this->request_type = $item['type'];
			$this->request_link = $item['value'];
			$this->request_link_class = $item['value'];
			
			$this->_status = 1;
		}
	}
	
	function _publish() {
		global $request_uri, $request_type, $request_link, $request_link_class, $request_link_subclass;
		global $request_param, $request_link_param;
		
		$request_uri = $this->request_uri;
		$request_type = $this->request_type;
		$request_link = $this->request_link;
		$request_link_class = $this->request_link_class;
		$request_link_subclass = $this->request_link_subclass;
		$request_param = $this->request_param;
		$request_link_param = $this->request_link_param;
	}
	
	//
	function _process_request_uri($has_get = false) {
		if( $this->request_link == '' ) {
			$_uri = $this->request_type;
		} else {
			$_uri = $this->request_type . '/' . str_replace(CONFIG_URL_SUFFIX, '', $this->request_link);
		}
		
		if( $has_get == true && is_array($_GET) ) {
			foreach($_GET as $k => $v) {
				$_uri .= '__' . $k . '_' . $v;
			}
		}
		
		return $_uri;
	}
}
//
?>